﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;
using System.Linq;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>

    public partial class YCAWDPLAN
    {
        bool success = true;
        string failreason = "";

        public void AUTOEK()
        {
            DataSet SResult;
            SResult =m_Data.CreateTempEKPOS(SetTmp.ICC_EKPOS);
            tpEKPOS.Clear();
            SetTmp.Clear();
            Set4ITX.ICC_EKPOS.Clear();
            Set4ITX.Merge(SResult);
            btnITXPurchase.IsEnabled = true;
        }
        public void LOADXDECK(DataRow dwInput, DataRow dwSrc, bool isPlanned, string sections, bool isClearEKPOS = true)
        {
            try
            {
                DataSet SResult;
                SResult = m_Data.LoadXDECKData(dwInput, isPlanned, sections);
                pSType = "";
                SetData.ABPOS.Clear();
                SetData.ICC_ABAUFM.Clear();
                cmbKRS.Text = "WP";
                foreach (DataRow rx in SResult.Tables["ICC_ABAUFM"].Rows)
                {
                    decimal qtyre = System.Convert.ToDecimal(rx["ABAQTYRECEIVED"].ToString());
                    if (qtyre > 0)//通过此标志，判断是否是补充采购。
                    {
                        cmbKRS.Text = "RP";
                        break;
                    }
                }
                //MBST EKPOS MDECK ABPOS ICC_ABAUFM
                if (SResult.Tables.Contains("ICC_STOCK"))
                {
                    Set4ITX.ICC_STOCK.Clear();
                    Set4ITX.Merge(SResult.Tables["ICC_STOCK"]);
                }
                if (isClearEKPOS)
                {
                    Set4ITX.ICC_EKPOS.Clear();
                    if (SResult.Tables.Contains("ICC_EKPOS"))
                    {
                        Set4ITX.Merge(SResult.Tables["ICC_EKPOS"]);
                    }
                }
                else
                {
                    if (SResult.Tables["ICC_EKPOS"].Rows.Count > 0)
                    {
                        var qekp = from DataRow row in SResult.Tables["ICC_EKPOS"].AsEnumerable()
                                   where row["EKPAUFKRS"].ToString() == dwSrc["EKPAUFKRS"].ToString()
                                   && row["EKPAUFLFD"].ToString() == dwSrc["EKPAUFLFD"].ToString()
                                   && row["EKPAUFPOS"].ToString() == dwSrc["EKPAUFPOS"].ToString()
                                   select row;

                        foreach (DataRow rwp in qekp)
                        {
                            dwSrc["EKPQTYLOCKED"] = rwp["EKPQTYLOCKED"];
                            dwSrc["EKPQTYFREE"] = rwp["EKPQTYFREE"];
                            break;
                        }
                    }
                }
                if (SResult.Tables.Contains("ABPOS"))
                {
                    SetData.Merge(SResult.Tables["ABPOS"]);
                }
                if (SResult.Tables.Contains("ICC_ABAUFM"))
                {
                    SetData.Merge(SResult.Tables["ICC_ABAUFM"]);
                }
                if (SResult.Tables.Contains("MX_ABPOS"))
                {
                    if (SetData.Tables.Contains("MX_ABPOS"))
                    {
                        SetData.Tables["MX_ABPOS"].Clear();
                    }
                    SetData.Merge(SResult.Tables["MX_ABPOS"]);
                }

                if (vwITXXDECK.View != null)
                {
                    vwITXXDECK.View.MoveCurrentToFirst();
                }
                if (vwITXABPOS.View != null)
                {
                    vwITXABPOS.View.MoveCurrentToFirst();
                }
                txtExtFactory.Text = "";
                var query = from row in SetData.Tables["ICC_ABAUFM"].AsEnumerable()
                            group row by row.Field<string>("LSLIEFNA") into grp
                            select new
                            {
                                Id = grp.Key,
                                sum = grp.Sum(r => r.Field<decimal>("ABARESMG"))
                            };
                foreach (var grp in query)
                {
                    txtExtFactory.Text += grp.Id + grp.sum + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}